package com.fubowen.reader.ui.download

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fubowen.reader.R
import com.fubowen.reader.adapter.SimpleListAdapter
import com.fubowen.reader.databinding.PublicCollectorBinding
import com.fubowen.reader.db.bean.WebSiteConfigBean
import com.fubowen.reader.ui.fragment.ThemeFragment

class CollectWebSiteFragment : ThemeFragment() {

    private lateinit var binding: PublicCollectorBinding
    private lateinit var adapter: SimpleListAdapter<WebSiteConfigBean>
    private lateinit var layoutManager: LinearLayoutManager
    private lateinit var recycler: RecyclerView

    override var isDialog = true

    //region onCreateView
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = PublicCollectorBinding.inflate(inflater)
        recycler = binding.filter
        layoutManager = recycler.layoutManager as LinearLayoutManager
        adapter = SimpleListAdapter(R.layout.public_collector_item, requireContext()) {}
        recycler.layoutManager = layoutManager
        recycler.adapter = adapter
        config {
            adapter.submitList(it)
        }
        binding.local.setOnClickListener { cacheViewModel.downloadSite(binding.siteName.toString(),false) }
        binding.remote.setOnClickListener { cacheViewModel.downloadSite(binding.siteName.toString(),true) }
        return binding.root
    }


    //endregion

    private fun config(submit: (List<WebSiteConfigBean>) -> Unit) {
        val siteRules = listOf(
            WebSiteConfigBean(
                label = "链接名称"
            ),
            WebSiteConfigBean(
                label = "链接地址"
            ),
            WebSiteConfigBean(
                label = "作品简介"
            ),
            WebSiteConfigBean(
                label = "匹配封面"
            ),
            WebSiteConfigBean(
                label = "匹配作者"
            ),
            WebSiteConfigBean(
                label = "作品状态"
            ),
            WebSiteConfigBean(
                label = "更新时间"
            ),
            WebSiteConfigBean(
                label = "匹配目录",
                value = "a:matches(目录|章节列表|返回目录)"
            ),
            WebSiteConfigBean(
                label = "匹配章节",
                value = "a:matches(第\\s*[一二三四五六七八九十零\\d]+\\s*章)"
            ),
            WebSiteConfigBean(
                label = "章节内容",
                value = ":has(>br:gt(10))"
            )
        )
        submit(siteRules)
    }
}

